#!/bin/bash

source /home/shell/util.sh

# 输入安装参数
printInfo "收集安装信息";
read -p "nacos web服务端口[8848]：" nacos_webserver_port;
nacos_webserver_port=${nacos_webserver_port:-"8848"};

let grpc_port=${nacos_webserver_port}+1000;
let grpc_sync_port=${nacos_webserver_port}+1001;

read -p "MySQL主机地址[${host_addr}]：" mysql_host;
mysql_host=${mysql_host:-${host_addr}};

read -p "MySQL用户名[dev_user]：" mysql_username;
mysql_username=${mysql_username:-"dev_user"};

read -p "MySQL密码[123456]：" mysql_passwd;
mysql_passwd=${mysql_passwd:-"123456"};

if [[ -z "${mysql_host}" ]] || [[ -z "${mysql_username}" ]] || [[ -z "${mysql_passwd}" ]];
then
  printFail "MySQL信息输入不完整，安装程序退出！";
  exit;
else
  printInfo "=============安装信息=============";
  printInfo "MySQL主机地址：$mysql_host\nMySQL用户名：$mysql_username\nMySQL密码：$mysql_passwd";
  printInfo "=============安装信息=============";
fi

printInfo "nacos版本列表："
curl -sL https://hub.fastgit.org/alibaba/nacos/releases/|grep '/alibaba/nacos/releases/tag/'|perl -pe "s/.*tag\/(.*)\".*/\1/g"
while [ true ]; do
    read -p "选择nacos版本：" version
    if [[ -z "${version}" ]]; then
        printFail "请重新输入版本！"
        continue;
    else
        break;
    fi
done

# 检测mysql客户端
requireMysqlCient;

# 检测Java环境
requireJava;


cd /opt
printInfo "获取nacos安装文件...";
# rz
if [ ! -f /opt/nacos-server.tar.gz ];
then
  printInfo "未检测到文件nacos-server.tar.gz，正尝试从github下载，下载速度可能较慢，请耐心等待。";
  download /opt/nacos-server.tar.gz https://ghproxy.com/https://github.com/alibaba/nacos/releases/download/${version}/nacos-server-${version}.tar.gz
fi

if [ ! -d "/opt/nacos" ];
then
  tar -xzf /opt/nacos-server.tar.gz
  rm -rf /opt/nacos-server.tar.gz
else
  printInfo "nacos目录存在！";
fi


# 导入数据库
printInfo "创建数据库";
mysql -u $mysql_username --password="$mysql_passwd" -h $mysql_host -e "drop database if exists nacos;create database nacos;"
mysql -u $mysql_username --password="$mysql_passwd" -h $mysql_host -e "use nacos;source /opt/nacos/conf/nacos-mysql.sql;"

# 修改配置
printInfo "修改配置";
sed -i "s/# db.num=1/db.num=1/g" /opt/nacos/conf/application.properties
sed -i "s/# db.url.0=jdbc:mysql:\/\/127.0.0.1:3306/db.url.0=jdbc:mysql:\/\/"$mysql_host":3306/g" /opt/nacos/conf/application.properties
sed -i "s/# db.user.0=nacos/db.user.0="$mysql_username"/g" /opt/nacos/conf/application.properties
sed -i "s/# db.password.0=nacos/db.password.0="$mysql_passwd"/g" /opt/nacos/conf/application.properties
sed -i "s/# spring.datasource.platform=mysql/spring.datasource.platform=mysql/g" /opt/nacos/conf/application.properties
sed -i "s/# nacos.inetutils.prefer-hostname-over-ip=false/nacos.inetutils.prefer-hostname-over-ip=true/g" /opt/nacos/conf/application.properties
sed -i "s/server.port=8848/server.port="${nacos_webserver_port}"/g" /opt/nacos/conf/application.properties
# sed -i "s/nacos.core.auth.enabled=false/nacos.core.auth.enabled=true/g" /opt/nacos/conf/application.properties

# 集群模式配置
tee /opt/nacos/conf/cluster.conf<<EOF
$host_addr
EOF

# 运行程序
# source /etc/profile.d/java.sh
# bash /opt/nacos/bin/startup.sh -m standalon

# 创建服务
ln -s /opt/jdk/bin/javac /usr/local/bin/javac
bash /home/shell/serviced-install.sh<<EOF
2
2

nacos-java
bash /opt/nacos/bin/startup.sh -m standalone


bash /opt/nacos/bin/shutdown.sh

EOF

# 添加防火墙
firewall-addport ${nacos_webserver_port}
firewall-addport ${grpc_port}
firewall-addport ${grpc_sync_port}

# 安装信息
printInfo "================安装信息================"
printInfo "控制台地址：http://${host_addr}:${nacos_webserver_port}/nacos"
printInfo "日志目录：/opt/nacos/logs"
printInfo "应用日志：/opt/nacos/logs/start.out"
printInfo "集群日志：/opt/nacos/logs/nacos-cluster.log "
printInfo "默认用户名：nacos "
printInfo "默认密码：nacos "
printInfo "================安装信息================"